package com.yami.shop.admin.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yami.shop.bean.model.Post;
import com.yami.shop.bean.param.PostsQuery;
import com.yami.shop.bean.vo.PostsVO;
import com.yami.shop.common.response.ResponseEnum;
import com.yami.shop.common.response.ServerResponseEntity;
import com.yami.shop.common.util.PageParam;
import com.yami.shop.service.PostService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author DELL
 */
@RestController
@Slf4j
@RequestMapping("/social/post")
public class PostsController {

    @Autowired
    private PostService postsService;

    /**
     * 分页查询动态
     */
    @GetMapping("/page")
    @PreAuthorize("@pms.hasPermission('social:post:page')")
    public ServerResponseEntity<IPage<PostsVO>> page(PostsQuery query, PageParam<Post> page) {
        return ServerResponseEntity.success(postsService.getPostPage(page, query));
    }

    /**
     * 审核操作
     */
    @PutMapping("/audit/{postId}")
//    @PreAuthorize("@pms.hasPermission('social:post:audit')")
    @PreAuthorize("true")
    public ServerResponseEntity<Void> audit(
            @PathVariable Long postId,
            @RequestParam String action,
            @RequestParam(required = false) String reason) {

        postsService.auditPost(postId, action, reason);
        return ServerResponseEntity.success();
    }

    @PostMapping("/delete")
    @PreAuthorize("@pms.hasPermission('social:post:delete')")
    public ServerResponseEntity delete(@RequestBody List<Long> postIds) {

        log.info("请求参数IDs: {}", postIds);
        if (postIds == null || postIds.isEmpty()) {
            return ServerResponseEntity.fail(ResponseEnum.valueOf("Post IDs list is empty."));
        }

        // 可以在这里进行更详细的权限验证或其他业务逻辑检查

        boolean result = postsService.removeBatchByIds(postIds);
        int deletedCount = result ? postIds.size() : 0; // 假设removeBatchByIds返回true表示全部删除成功

        Map<String, Object> response = new HashMap<>();
        response.put("success", result);
        response.put("deletedCount", deletedCount);

        if(result){
            return ServerResponseEntity.success(response);
        } else {
            return ServerResponseEntity.fail(ResponseEnum.valueOf("Failed to delete posts."));
        }
    }
    /**
     * 获取动态详情
     */
    @GetMapping("/{postId}")
    @PreAuthorize("@pms.hasPermission('social:post:info')")
    public ServerResponseEntity<Post> info(@PathVariable Long postId) {
        Post post = postsService.getById(postId);
        return ServerResponseEntity.success(post);
    }
    /**
     * 修改
     */
    @PutMapping
    @PreAuthorize("@pms.hasPermission('social:post:update')")
    public ServerResponseEntity<Void> update(@RequestBody Post post) {
        postsService.updateById(post);
        return ServerResponseEntity.success();
    }
}
